2
2
.
.
7
7
.
.
4
4
@
@
O
O
n
n
e
e
T
T
o
o
M
M
a
a
n
n
y
y
-
-
F
F
o
o
r
r
e
e
i
i
g
g
n
n
K
K
e
e
y
y
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to implement @OneToMany relationship (between Author and Books).
Single Author can be related to many Books - by adding Foreign Key Column authorId to Book Table.
Compared to @OneToMany - Join Table we have only add following two lines
In Book.java public Integer authorId; (add Column to store Foreign Key to Author)
In Author.java @JoinColumn(name = "authorId") (specify which Book Column to use as Foreign Key)
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 Database
DB Schema
AUTHOR
AUTHOR_ID
NAME
AGE
BOOK
TITLE
ID
ID
1 : X
MyController
http://localhost:8080/AddAuthor
Author
AuthorRepository
addAuthor()
Book
Book.java
public Integer authorId; //Add Column to Book to store Foreign Key to Author
Author.java
@OneToMany(cascade = CascadeType.ALL) //Specify Relationship (By default uses Join-Table)
@JoinColumn(name = "authorId") //Specify Foreign Key Column in Book (Overrides default behaviour)
public Set<Book> books; //Store related Books when getting Author from DB
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_relationships_onetoone_foreignkey (add Spring Boot Starters from the table)
Edit FIle: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
Create Class: Author.java (inside package entities)
Create Class: Book.java (inside package entities)
Create Package: repositories (inside main package)
Create Interface: AuthorRepository.java (inside package repositories)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
application.properties
spring.datasource.url = jdbc:h2:mem:testdb
spring.h2.console.enabled = true
Author.java
package com.ivoronline.springboot_relationships_onetomany_foreignkey.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import java.util.Set;
@Entity
public class Author {
//PRIMARY KEY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
//RELATIONSHIPS
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "authorId")
public Set<Book> books;
//DATA
public String name;
public Integer age;
}
AuthorRepository.java
package com.ivoronline.relationships_onetomany.repositories;
import com.ivoronline.relationships_onetomany.entities.Author;
import org.springframework.data.repository.CrudRepository;
public interface AuthorRepository extends CrudRepository<Author, Integer> { }
Book.java
package com.ivoronline.springboot_relationships_onetomany_foreignkey.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
//PRIMARY KEY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
//FOREIGN KEY
public Integer authorId;
//DATA
public String title;
}
MyController.java
package com.ivoronline.relationships_onetomany.controllers;
import com.ivoronline.relationships_onetomany.entities.Author;
import com.ivoronline.relationships_onetomany.entities.Book;
import com.ivoronline.relationships_onetomany.repositories.AuthorRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashSet;
@Controller
public class MyController {
@Autowired
AuthorRepository authorRepository;
@ResponseBody
@RequestMapping("/AddAuthor")
public String addAuthor() {
//CREATE BOOK ENTITIES
Book book1 = new Book();
book1.title = "Book about dogs";
Book book2 = new Book();
book2.title = "Book about cats";
//CREATE AUTHOR ENTITY
Author author = new Author();
author.name = "John";
author.age = 20;
author.books = new HashSet<Book>();
author.books.add(book1);
author.books.add(book2);
//STORE AUTHOR/BOOKS ENTITIES
authorRepository.save(author);
//RETURN SOMETHING TO BROWSER
return "Author & Books were stored into DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddAuthor
AUTHOR BOOK http://localhost:8080/h2-console
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>